package com.abl.core.mybatis.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

import java.util.Collection;
import java.util.List;

public interface ZBMapper {

    @Insert({
            "<script>",
            " INSERT INTO ${tableName} ",
            " ",
            " <foreach collection='columns' item='column' open='(' separator=',' close=')'> ",
            " `${@com.abl.core.mybatis.util.DbKit@humpTo_(column)}` ",
            " </foreach> ",
            " ",
            " VALUES ",
            " ",
            " <foreach collection='entityList' item='entityItem' separator=','> ",
            "     <foreach collection='columns' item='column' open='(' separator=',' close=')'> ",
            "        #{entityItem.${column}} ",
            "     </foreach> ",
            " </foreach> ",
            " ",
            "</script>"
    })
    long saveBatch(@Param("tableName") String tableName,
                   @Param("columns") Collection<?> columns,
                   @Param("entityList") List<?> entityList);

//    @Insert({
//            "<script>",
//            "  INSERT INTO ${tableName} ",
//            " ",
//            " <foreach collection='columns' item='column' open='(' separator=',' close=')'> ",
//            "    `${column}` ",
//            " </foreach> ",
//            " ",
//            " VALUES ",
//            " ",
//            " <foreach collection='columns' item='key' index='index' open='(' separator=',' close=')'> ",
//            "    #{entity.${key}} ",
//            " </foreach> ",
//            " ",
//            "</script>"
//    })
//    long save(@Param("tableName") String tableName, @Param("columns") Collection<?> columns, @Param("entity") Object entity);

    @Delete({
            "<script>",
            " DELETE FROM ${tableName} ",
            " WHERE `${idColumn}` in ",
            " ",
            " <foreach item='value' index='index' collection='ids' open='(' separator=',' close=')'>",
            "    #{value} ",
            " </foreach> ",
            " ",
            "</script>"
    })
    long deleteByKeys(@Param("tableName") String tableName, @Param("idColumn") String idColumn, @Param("ids") Collection<?> ids);

    @Delete({
            "<script>",
            " DELETE FROM ${tableName} WHERE ${sqlStr} ",
            "</script>"
    })
    long deleteByStr(@Param("tableName") String tableName, @Param("sqlStr") String sqlStr);

    @Update({
            "<script>",
            " UPDATE ${tableName} SET ",
            " ",
            " <foreach collection='columns' item='column' separator=',' > ",
            " `${@com.abl.core.mybatis.util.DbKit@humpTo_(column)}` = #{entity.${column}} ",
            " </foreach> ",
            " WHERE ${whereSql}",
            "</script>"
    })
    long update(@Param("tableName") String tableName,
                @Param("columns") Collection<?> columns,
                @Param("entity") Object entity,
                @Param("whereSql") String whereSql
    );


    @Update({
            "<script>",
            " UPDATE ${tableName} SET ",
            " ",
            " <foreach collection='columns' item='column' separator=',' > ",
            " `${@com.abl.core.mybatis.util.DbKit@humpTo_(column)}` = #{entity.${column}} ",
            " </foreach> ",
            " ",
            " WHERE `${idColumn}` in ",
            " ",
            " <foreach item='value' index='index' collection='ids' open='(' separator=',' close=')'>",
            "    #{value} ",
            " </foreach> ",
            " ",
            "</script>"
    })
    long updateByKeys(@Param("tableName") String tableName,
                      @Param("columns") Collection<?> columns,
                      @Param("entity") Object entity,
                      @Param("idColumn") String idColumn,
                      @Param("ids") Collection<?> ids
    );


//    <!--  update  -->
//    <update id="_updateById_">
//
//            <foreach collection="entity.keys" item="key" separator=",">
//            `${key}` = #{entity[${key}]}
//        </foreach>
//    WHERE ${idColumn}=#{id}
//    </update>

//
//    <!--    <update id="_updateOrigin_">-->
//    <!--        ${sqlString}-->
//    <!--    </update>-->
//    <update id="_update_">
//    UPDATE ${tableName} SET
//            <!-- 判断条件参数是否为空 -->
//        <if test="sqlSet != null and sqlSet != ''">
//    ${sqlSet}
//        </if>
//
//        <if test="entity != null">
//            <foreach collection="entity.keys" item="key" separator=",">
//            `${key}` = #{entity[${key}]}
//            </foreach>
//        </if>
//    WHERE ${ew.sqlSegment}
//    </update>
//    <update id="_updateBatchById_" useGeneratedKeys="true" keyProperty="entityList.id">
//    update ${tableName} set
//            <foreach collection='columns' item='column' separator=",">
//            `${column}` = case id
//            <trim suffix="end">
//                <foreach collection="entityList" item="entity">
//    when #{entity.${idColumn}} then #{entity.${column}}
//                </foreach>
//            </trim>
//        </foreach>
//    where ${idColumn} in
//            <foreach collection="entityList" item="entity" separator="," open="(" close=")">
//            #{entity.${idColumn}}
//        </foreach>
//    </update>


}
